package com.cwx.mychat.openai.factory;

import com.cwx.mychat.entity.dto.ModelConfigDto;
import com.cwx.mychat.entity.enums.AIModelTypeEnum;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.stereotype.Component;

/**
 * 作者： cwx
 * 时间： 2025/5/27 11:53
 * 版本： 1.0
 * 注释：
 */
public class OpenAIModelFactory implements ModelFactory<OpenAiChatModel> {
    @Override
    public boolean support(String modelType) {
        return AIModelTypeEnum.OPENAI.getType().equalsIgnoreCase(modelType);
    }

    @Override
    public OpenAiChatModel create(ModelConfigDto modelConfigDto) {
        return OpenAiChatModel.builder()
                .openAiApi(
                        modelConfigDto.getBaseUrl() != null? OpenAiApi.builder()
                                .apiKey(modelConfigDto.getApiKey())
                                .baseUrl(modelConfigDto.getBaseUrl())
                                .build()
                                : OpenAiApi.builder()
                                .apiKey(modelConfigDto.getApiKey())
                                .build()
                )
                .defaultOptions(OpenAiChatOptions.builder()
                        .model(modelConfigDto.getAiModel())
                        .temperature(modelConfigDto.getTemperature())
                        .maxTokens(modelConfigDto.getMaxTokens())
                        .topP(modelConfigDto.getTopP())
                        .build())
                .build();
    }
}
